package com.fgsecure.apjhnou.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;

import com.fgsecure.apjhnou.app.R;

public class EditItemAdapter extends ArrayAdapter<String> {
	private LayoutInflater mInflater;
	private static LayoutInflater inflater = null;
	int layoutResourceId;
	Context context;
	public ArrayList myItems = new ArrayList();
	String data[] = null;

	public EditItemAdapter(Context context, int layoutResourceId, String[] data) {
		super(context, layoutResourceId, data);

		this.layoutResourceId = layoutResourceId;
		this.context = context;
		for (int i = 0; i < 20; i++) {
			ListItem listItem = new ListItem();
			listItem.caption = "Caption" + i;
			myItems.add(listItem);
		}
		notifyDataSetChanged();
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			convertView = inflater.inflate(layoutResourceId, parent, false);
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.row_edit_item, null);
			holder.caption = (EditText) convertView
					.findViewById(R.id.ItemCaption);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		// Fill EditText with the value you have in data source
		// String str = data[position];
		holder.caption.setText(myItems.get(position).toString());
		holder.caption.setId(position);

		// we need to update adapter once we finish with editing
		holder.caption.setOnFocusChangeListener(new OnFocusChangeListener() {
			public void onFocusChange(View v, boolean hasFocus) {
				if (!hasFocus) {
					final int position = v.getId();
					final EditText Caption = (EditText) v;
					myItems.set(position, Caption.getText().toString());
				}
			}
		});

		return convertView;
	}

	class ViewHolder {
		EditText caption;
	}

	class ListItem {
		String caption;
	}
}
